﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ExamSharedClasses {
    /// <summary>
    /// (k) Der Antworttyp passend zur Frage mit Textantwort.
    /// </summary>
    [Serializable()]
    public class AnswerTypeText : Answer {
        new public const string TABLENAME = "answer_type_text";
        public const string ANSWER_USER_ANSWER = "answer_user_answer";

        public string AnswerUserAnswer { get; set; }

        public AnswerTypeText()
            : base() {
            this.AnswerUserAnswer = "";
        }
        public AnswerTypeText( int examId, Question question ) : this() {
            AnswerExamId = examId;
            AnswerQuestion = question;
        }
        public AnswerTypeText( Answer answer )
            : base( answer ) {
            this.AnswerUserAnswer = "";
        }
        public AnswerTypeText( Answer answer, string answerUserAnswer )
            : base( answer ) {
            this.AnswerUserAnswer = answerUserAnswer;
        }

        public override string ToMoreDetailedString() {
            StringBuilder sb = new StringBuilder( base.ToMoreDetailedString() + "AnswerTypeText: " );
            if ( AnswerUserAnswer != null )
                sb.Append( "ANSWER: " + AnswerUserAnswer + '\n' );
            return sb.ToString();
        }
        public override string ToString() {
            return "angegeben: " + AnswerUserAnswer + '\n';
        }
        public override int GetAchievedPoints( int maxPointsInt ) {
            double maxPoints = maxPointsInt, achievedPoints = 0.0;
            QuestionTypeText qtm = ( QuestionTypeText )AnswerQuestion;
            string userSolution = AnswerUserAnswer, solution = qtm.QuestionRightAnswer;
            solution = Tools.GetRidOfCertainCharacters( solution );
            userSolution = Tools.GetRidOfCertainCharacters( userSolution );
            if ( solution.Equals( userSolution ) )
                achievedPoints = maxPoints;
            return ( int )achievedPoints;
        }

        public override bool HasSetAnswer() {
            return ( AnswerUserAnswer!=null && !AnswerUserAnswer.Equals(""));
        }
        public override void SetDefaultAnswer() {
            AnswerUserAnswer = "-";
        }
        public override bool HasEmptyAnswer() {
            return !HasSetAnswer();
        }
    }
}
